//
//  AnuViewController.m
//  TinyFortune
//
//  Created by lc on 16/6/11.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "AnuViewController.h"
#import "anuTableViewCell.h"
#import "AnuDetailViewController.h"

NSString *const anuIdentifier = @"anuCell";

@interface AnuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *anuArray;
@end


@implementation AnuViewController
#pragma 懒加载
- (NSMutableArray *)anuArray{
    if (!_anuArray) {
        _anuArray = [NSMutableArray new];
    }else{
        [self sort:_anuArray];
    }
    return _anuArray;
}
#pragma mark - 排序
- (void)sort:(NSMutableArray *)array{
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO];
    [array sortUsingDescriptors:@[sort]];
}
#pragma mark - createUI
- (void)createUI{
    //实例化导航条
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(onClicked)];
    [self.navigationItem setLeftBarButtonItem:leftBar];
    self.navigationController.navigationBar.translucent = NO ;
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5]];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self setTitle:@"纪念日"];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"add"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];
    [self.navigationItem setRightBarButtonItem:rightBar];
    //添加滑动手势
    UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightGo)];
    [self.view addGestureRecognizer:rightSwip];
    [rightSwip setDirection:UISwipeGestureRecognizerDirectionRight];

    //tableView实例化
    _anuTableView.delegate = self ;
    _anuTableView.dataSource = self ;
    [_anuTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_anuTableView registerNib:[UINib nibWithNibName:@"anuTableViewCell" bundle:nil] forCellReuseIdentifier:anuIdentifier];
    
   
}
#pragma mark - tableView协议
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.anuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.anuArray[indexPath.row];
    anuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:anuIdentifier forIndexPath:indexPath];
    cell.timeLabel.text = dict[@"time"];
    cell.contentLabel.text = dict[@"content"];
    if (![[AVUser currentUser].mobilePhoneNumber isEqualToString:dict[@"user"]]) {
        cell.tView.backgroundColor = [UIColor colorWithRed:255/255.0f green:54/255.0f blue:103/255.0f alpha:1];
        cell.timeLabel.textColor =[UIColor colorWithRed:255/255.0f green:54/255.0f blue:103/255.0f alpha:1];
    }
   [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AnuDetailViewController *anuDetail = [AnuDetailViewController new];
    anuDetail.isFirst = YES ;
    anuDetail.dic = self.anuArray[indexPath.row];
    [self.navigationController pushViewController:anuDetail animated:YES];
}
#pragma mark - 添加左滑手势
- (void)rightGo{
    [self onClicked];
}

- (void)onClicked{
 [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)addAction{
    AnuDetailViewController *anuDetail = [AnuDetailViewController new];
    [self.navigationController pushViewController:anuDetail animated:YES];
}
#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated   {
    [super viewDidAppear:animated];
    [self requestData];

}

#pragma mark - 数据相关
- (void)requestData{
    [self.anuArray removeAllObjects];
    AVUser *user = [AVUser currentUser];
    //从日记表单查询到需要的数据
    AVQuery *query = [AVQuery queryWithClassName:@"diary"];
    [query whereKey:@"user" containedIn:@[user.mobilePhoneNumber,[user objectForKey:@"lovers"]]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        NSDictionary *dic = [NSDictionary new];
        for (AVObject *object in objects) {
            //手动解析
            dic = @{@"time":[object objectForKey:@"time"],
                    @"user":[object objectForKey:@"user"],
                    @"content":[object objectForKey:@"content"]};
            [self.anuArray addObject:dic];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
      
            [self.anuTableView reloadData];
        });

    }];
    
}
@end
